<?php

/**
* 商品控制器
*/
class GoodsController extends AuthController{
	public $model;
	
	/*自动执行
	 **/
	public function __init(){
	    parent::__init();
		$this->model=K('Goods');
	}
    /*
     * 商品列表
     */
    public function index(){
        $this->assign('goodsData',$this->model->all());
        $this->display();
    }
	
	/*添加商品
	 **/
	public function addGoods(){
		if(IS_POST){
			$this->model->addGoods();
			$this->success('添加成功',U('index'));
		}
	    //获得商品分类数据
		$this->assign('cateData',Data::tree(M('category')->all(),'cname'));
		//获得商品品牌数据
		$this->assign('brandData',M('brand')->all());
		$this->display();
	}
	/*修改商品
	 **/
	public function editGoods(){
		if(IS_POST){
		    $this->model->editGoods();
			$this->success('修改成功',U('index'));
		}
	//**获取被编辑的数据
		$gid=Q('get.gid',0,'intval');
		$goodsData=M()->join("__goods__ as g join __goods_detail__ as gd on g.gid=gd.goods_gid")->where("gid={$gid}")->find();
		$goodsData['bimg']=explode('|',trim($goodsData['bimg'],'|'));
		$this->assign('goodsData',$goodsData);
		//获得该商品的选择的属性
		$goodsAttr=M()->join("__goods_attr__ as ga join __type_attr__ as ta on ga.tp_aid=ta.tp_aid")
					  ->where("ga.goods_gid={$gid} and ta.tp_aclass=0")->getField('attr_value',true);
		$this->assign('goodsAttr',$goodsAttr);
		//获得该商品的选择的规格
		$goodsSpec=M()->join("__goods_attr__ as ga join __type_attr__ as ta on ga.tp_aid=ta.tp_aid join type as t on ta.tpid=t.tpid")
					  ->where("goods_gid={$gid} and tp_aclass=1")->all();
		$arr=array();	
		foreach ($goodsSpec as $k => $v) {
			$arr[$goodsSpec[$k]['tp_aid']][]=$v;
		}
		$this->assign('goodsSpec',$arr);
		//获得所有分类
		$this->assign('cateData',Data::tree(M('category')->all(),'cname'));
		//获得所有品牌
		$this->assign('brandData',M('brand')->all());
		//获得该商品分类下的所有属性（为了显示出所有属性进行属性添加）
		//1、商品的固有属性
		$attr=M('type_attr')->where("tpid={$goodsData['tpid']} and tp_aclass=0")->all();
		foreach ($attr as $k => $v) {
			$attr[$k]['tp_avalue']=explode(',',$v['tp_avalue']);
		}
		$this->assign('attr',$attr);
		//1、商品的规格属性
		$spec=M('type_attr')->where("tpid={$goodsData['tpid']} and tp_aclass=1")->all();
		foreach ($spec as $k => $v) {
			if($v['tp_avalue']=='') continue;
			$spec[$k]['tp_avalue']=explode(',',$v['tp_avalue']);
		}
		$this->assign('spec',$spec);
	    $this->display();
	}
	
	
	
	/*删除商品
	 **/
	public function del(){
	    $gid=Q('get.gid',0,'intval');
	    //删除列表图
	    $listImg=M('goods')->where("gid={$gid}")->getField('list_img');
	    //如果列表图不是默认图，删除图片
	    if(basename($listImg)!='default.jpg') is_file($listImg)&&unlink($listImg);
		//删除商品图册
		$showImg=M('goods_detail')->where("goods_gid={$gid}")->getField('bimg');
		if($showImg!=''){
			$showImg=explode('|',trim($showImg,'|'));
			foreach ($showImg as $i) {
				$type=strrchr($i,'.');
				$small=str_replace($type, 'small'.$type, $i);
				$mid=str_replace($type, 'mid'.$type, $i);
				is_file($i)&&unlink($i);
				is_file($small)&&unlink($small);
				is_file($mid)&&unlink($mid);
			}
		}
		//删除商品表数据
		M('goods')->where("gid={$gid}")->delete();
		//删除商品属性表数据
		M('goods_attr')->where("goods_gid={$gid}")->delete();
		//删除商品详细表数据
		M('goods_detail')->where("goods_gid={$gid}")->delete();
		$this->success('删除成功',U('index'));
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
//************AJAX处理区域*********************
	/*ajax获得商品属性
	 **/
	public function AjaxGetAttr(){
		if(IS_AJAX){
			$tpid=Q('post.tpid',0,'intval');
			$attrData=M('type_attr')->where("tpid={$tpid}")->all();
			foreach ($attrData as $k => $v) {
				$attrData[$k]['tp_avalue']=explode(',',$v['tp_avalue']);
			}
			$this->ajax($attrData);
		}
	}
	
	 /**
     * uploadify上传列表图
     */
    public function listImg()
    {
        $upload = new Upload('Upload/listImgTemp/');
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
            $data = $file[0];
            $this->ajax($data);
        }
    }
	
	 /**
     * uploadify上传列表图
     */
    public function showImg()
    {
        $upload = new Upload('Upload/showImgTemp/');
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
            $data = $file[0];
            $this->ajax($data);
        }
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
    
	
	
	
	
    
    
?>